package com.share51.novel.job;

import com.share51.novel.common.StatusConstant;
import com.share51.novel.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;
import java.util.concurrent.Future;

/**
 * 小说正文内容爬虫
 * <p>根据数据库存储的小说章节数据，爬取小说的正文</p>
 * @author gb
 */
@Slf4j
@DisallowConcurrentExecution
public class CrawlChapterJob extends QuartzJobBean {

    @Resource
    private TaskService taskService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        boolean isRunning = jobExecutionContext.getTrigger().getJobDataMap().getBoolean(StatusConstant.QUARTZ_RUNNING_STATUS);
        if (!isRunning){
            jobExecutionContext.getTrigger().getJobDataMap().put(StatusConstant.QUARTZ_RUNNING_STATUS,true);
            log.info("开始爬取小说正文录入到数据库中");            long startTime = System.currentTimeMillis();
            try {
                taskService.crawlChapter();
                long endTime = System.currentTimeMillis();
                log.info("爬取小说正文录入到数据库中操作结束,耗时：{}秒",(endTime-startTime)/1000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                jobExecutionContext.getTrigger().getJobDataMap().put(StatusConstant.QUARTZ_RUNNING_STATUS,false);
            }
        }
    }
}
